package com.lecloud.util;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import cn.edu.live.status.RespStatus;

import com.alibaba.fastjson.JSONObject;

/**
 * 用于发送请求
 * 
 * @author zhagz
 * 
 */
public class Sender {

	/**
	 * 用于一般post的使用
	 * 
	 * @param url
	 * @param data
	 * @return
	 */
	public static JSONObject post(String url, Map<String, String> data) {
		Response resp = null;
		try {
			resp = Jsoup.connect(url).data(data)
					.header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8").ignoreContentType(true)
					.timeout(20000).method(Method.POST).execute();
			// System.out.println(resp.body());
			if (resp.statusCode() == RespStatus.SUCCESS) {
				JSONObject js = JSONObject.parseObject(resp.body());
				return js;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 用于一般的get方法
	 * 
	 * @param url
	 * @param data
	 * @return
	 */
	public static JSONObject get(String url, Map<String, String> data) {
		Response resp = null;
		try {
			resp = Jsoup.connect(url).data(data).ignoreContentType(true).timeout(20000).method(Method.GET).execute();
			// System.out.println(resp.body());
			if (resp.statusCode() == RespStatus.SUCCESS) {
				JSONObject js = JSONObject.parseObject(resp.body());
				return js;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
